package com.ruoyi.web.controller.book;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.MyBook;
import com.ruoyi.system.domain.MyBookType;
import com.ruoyi.system.domain.vo.MyBookQueryVo;
import com.ruoyi.system.service.IMyBookService;
import com.ruoyi.system.service.IMyBookTypeService;
import com.ruoyi.system.validate.group.AddData;
import com.ruoyi.system.validate.group.EditData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/book")
public class BookController extends BaseController {

    @Autowired
    private IMyBookService myBookService;

    @GetMapping("/all")
    public TableDataInfo getAllBooks(MyBookQueryVo myBookQueryVo){

        startPage();

        List<MyBook> list = myBookService.getAll(myBookQueryVo);

        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('library:book:add')")
    @PostMapping("/add")
    public AjaxResult addMyBook(@Validated(AddData.class) @RequestBody MyBook myBook){
        Long userId = SecurityUtils.getLoginUser().getUserId();

        myBook.setBelongTo(userId);
        boolean save = myBookService.save(myBook);
        return save ? AjaxResult.success("新增成功",myBook) : AjaxResult.error("新增失败");
    }

    @GetMapping("/get/{id}")
    public AjaxResult getMyBookById(@PathVariable("id") Integer id){
        MyBook myBook = myBookService.getByIdWithSub(id);

        return AjaxResult.success(myBook);
    }
    /*@GetMapping("/get/{id}")
    public AjaxResult getMyBookById(@PathVariable("id") String id){

        MyBook byId = myBookService.getById(id);

        return AjaxResult.success(byId);
    }*/
    @PutMapping("/edit")
    public AjaxResult editMyBook(@Validated(EditData.class) @RequestBody MyBook myBook){
        boolean update = myBookService.updateById(myBook);
        return update?AjaxResult.success("修改成功"):AjaxResult.error("修改失败");
    }
    @DeleteMapping("/delete/{bookIds}")
    public AjaxResult deleteByBookIds(@PathVariable Integer[] bookIds){
        boolean result = myBookService.removeByIds(Arrays.asList(bookIds));
        return result?AjaxResult.success("删除成功"):AjaxResult.error("删除失败");
    }

    @PostMapping("/importBook")
    public AjaxResult importBook(MultipartFile file) throws Exception {

        ExcelUtil<MyBook> excelUtil=new ExcelUtil<>(MyBook.class);

        List<MyBook> books = excelUtil.importExcel(file.getInputStream());

        boolean result = myBookService.saveBatch(books);

        return result ? AjaxResult.success("导入数据成功") :AjaxResult.error("导入数据失败");
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<MyBook> util = new ExcelUtil<MyBook>(MyBook.class);
        util.importTemplateExcel(response, "书本信息");
    }

    @PostMapping("/export")
    public void exportMyBooks(MyBook myBook, HttpServletResponse response){
        List<MyBook> list = myBookService.list();
        ExcelUtil<MyBook> excelUtil=new ExcelUtil<>(MyBook.class);

        excelUtil.exportExcel(response,list,"书本信息");
    }
    @GetMapping("/statistic/subscribe")
    public AjaxResult statisticSubScribe(){

        List<Map<String, Integer>> data = myBookService.getSubScribeCount();

        return AjaxResult.success(data);
    }

    @Autowired
    private IMyBookTypeService bookTypeService;

    @GetMapping("/type/list")
    public AjaxResult listAllBookType(){
        List<MyBookType> list = bookTypeService.list();
        return AjaxResult.success(list);
    }
}
